package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.CustomerService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: zhao
 * @Date: 2020/12/3 10:10
 */
@Api(description = "客户管理")
@RestController
@RequestMapping("/customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 客户列表分页（名称模糊查询）
     * <p>
     * 请求URL：http://localhost:8080 /customer/list
     * 请求参数：Integer page, Integer rows, String  customerName
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：Map<String,Object>
     */
    @PostMapping("/list")
    public Map<String, Object> getCustomer(Integer page, Integer rows, String customerName) {

        List<Customer> customerList = customerService.getCustomer(page, rows, customerName);
        //分页对象（里面有分页信息）
        PageInfo<Customer> pageInfo = new PageInfo<>(customerList);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("rows", customerList);
        return map;
    }

    /**
     * 客户添加或修改
     * 请求URL：http://localhost:8080/ customer/save?customerId=1
     * 请求参数：Customer customer
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("/save")
    public ServiceVO addCustomer(Customer customer) {
        return customerService.addCustomer(customer);
    }

    /**
     * 客户删除（支持批量删除）
     * 请求URL：http://localhost:8080/customer/delete
     * 请求参数：String  ids
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("/delete")
    public ServiceVO deleteCustomer(String ids) {
        return customerService.deleteCustomer(ids);
    }
}
